using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using WorkflowProcesses = ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses;
using WorkflowDataTypes = ScenarioFramework.WorkflowEngine.DataModel.WorkflowDataTypes;

namespace ScenarioFramework.Modeler.Forms.BPMN
{
	/// <summary>
	/// Descrizione di riepilogo per frmTransition.
	/// </summary>
	public class MessageFlow : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox txtMessageName;
		private System.Windows.Forms.TextBox txtToActivity;
		private System.Windows.Forms.TextBox txtFromActivity;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnCancel;
		/// <summary>
		/// Variabile di progettazione necessaria.
		/// </summary>
		private System.ComponentModel.Container components = null;

        private CheckBox chkViewName;
        private Button btnXML;
        private Button btnExtendedAttributes;

		WorkflowProcesses.XMLMessage xmlMessage;

		public MessageFlow()
		{
			InitializeComponent();

            xmlMessage = new WorkflowProcesses.XMLMessage();
		}

        /// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MessageFlow));
            this.txtMessageName = new System.Windows.Forms.TextBox();
            this.txtToActivity = new System.Windows.Forms.TextBox();
            this.txtFromActivity = new System.Windows.Forms.TextBox();
            this.panel2 = new System.Windows.Forms.Panel();
            this.btnExtendedAttributes = new System.Windows.Forms.Button();
            this.btnXML = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.chkViewName = new System.Windows.Forms.CheckBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.panel2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // txtMessageName
            // 
            this.txtMessageName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtMessageName.Location = new System.Drawing.Point(52, 195);
            this.txtMessageName.Name = "txtMessageName";
            this.txtMessageName.Size = new System.Drawing.Size(168, 20);
            this.txtMessageName.TabIndex = 22;
            this.txtMessageName.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // txtToActivity
            // 
            this.txtToActivity.BackColor = System.Drawing.Color.White;
            this.txtToActivity.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtToActivity.Enabled = false;
            this.txtToActivity.Location = new System.Drawing.Point(52, 308);
            this.txtToActivity.Name = "txtToActivity";
            this.txtToActivity.Size = new System.Drawing.Size(168, 20);
            this.txtToActivity.TabIndex = 17;
            this.txtToActivity.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // txtFromActivity
            // 
            this.txtFromActivity.BackColor = System.Drawing.Color.White;
            this.txtFromActivity.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtFromActivity.Enabled = false;
            this.txtFromActivity.Location = new System.Drawing.Point(52, 71);
            this.txtFromActivity.Name = "txtFromActivity";
            this.txtFromActivity.Size = new System.Drawing.Size(168, 20);
            this.txtFromActivity.TabIndex = 16;
            this.txtFromActivity.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // panel2
            // 
            this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel2.Controls.Add(this.btnExtendedAttributes);
            this.panel2.Controls.Add(this.btnXML);
            this.panel2.Controls.Add(this.btnCancel);
            this.panel2.Controls.Add(this.btnSave);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 408);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(367, 54);
            this.panel2.TabIndex = 27;
            // 
            // btnExtendedAttributes
            // 
            this.btnExtendedAttributes.Location = new System.Drawing.Point(156, 8);
            this.btnExtendedAttributes.Name = "btnExtendedAttributes";
            this.btnExtendedAttributes.Size = new System.Drawing.Size(60, 35);
            this.btnExtendedAttributes.TabIndex = 7;
            this.btnExtendedAttributes.Text = "Attributes";
            this.btnExtendedAttributes.UseVisualStyleBackColor = true;
            this.btnExtendedAttributes.Click += new System.EventHandler(this.btnExtendedAttributes_Click);
            // 
            // btnXML
            // 
            this.btnXML.Location = new System.Drawing.Point(288, 8);
            this.btnXML.Name = "btnXML";
            this.btnXML.Size = new System.Drawing.Size(60, 35);
            this.btnXML.TabIndex = 6;
            this.btnXML.Text = "XML";
            this.btnXML.UseVisualStyleBackColor = true;
            this.btnXML.Click += new System.EventHandler(this.btnXML_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(16, 8);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(60, 35);
            this.btnCancel.TabIndex = 5;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(222, 8);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(60, 35);
            this.btnSave.TabIndex = 4;
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // chkViewName
            // 
            this.chkViewName.Checked = true;
            this.chkViewName.CheckState = System.Windows.Forms.CheckState.Checked;
            this.chkViewName.Location = new System.Drawing.Point(266, 13);
            this.chkViewName.Name = "chkViewName";
            this.chkViewName.Size = new System.Drawing.Size(101, 21);
            this.chkViewName.TabIndex = 28;
            this.chkViewName.Text = "View in Canvas";
            this.chkViewName.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // pictureBox1
            // 
            this.pictureBox1.BackColor = System.Drawing.Color.White;
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(17, 13);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(234, 373);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 15;
            this.pictureBox1.TabStop = false;
            // 
            // MessageFlow
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(367, 462);
            this.ControlBox = false;
            this.Controls.Add(this.chkViewName);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.txtMessageName);
            this.Controls.Add(this.txtToActivity);
            this.Controls.Add(this.txtFromActivity);
            this.Controls.Add(this.pictureBox1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "MessageFlow";
            this.ShowInTaskbar = false;
            this.Text = "MessageFlow";
            this.panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if ( this.txtMessageName.Text == "" )
			{
                this.txtMessageName.Text = Guid.NewGuid().ToString();
				
			}

            /*
            if (this.chkViewName.Checked)
            {
                m_Shape.Text = this.txtMessageName.Text;
            }
            else
            {
                m_Shape.Text = string.Empty;
            }
            */
            xmlMessage.From = this.txtFromActivity.Text;
            xmlMessage.To = this.txtToActivity.Text;
            xmlMessage.Id = this.txtMessageName.Text;
            //m_Shape.Data1 = xmlMessage.GetXML();

			this.Close();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
            xmlMessage = null;
			this.Close();
		}

		public void BindObject(/*Microsoft.Office.Interop.Visio.Shape oShape*/)
		{
            /*
			m_Shape=oShape;
			if (oShape.Data1!="")
			{
                xmlMessage.Parse(oShape.Data1);
			}

            this.txtFromActivity.Text = xmlMessage.From;
            this.txtToActivity.Text = xmlMessage.To;

			Visio.Connects o_Connects=m_Shape.Connects;
			
			foreach (Visio.Connect o_Connect in o_Connects)
			{
				int FlowDirection=o_Connect.FromPart;
				string sShapeId = Scenario.Modeler.Utility.GetMasterNameUBase(o_Connect.ToSheet.Master.NameU);
				switch (sShapeId) 
				{
					case BPMNUtils.Start_MASTER:
					{
						switch (FlowDirection)
						{
							case 9:
								this.txtFromActivity.Text="Start";
								break;
							case 12:
								//TODO: Error
								break;
						}
						break;
					}

                    case BPMNUtils.End_MASTER:
								
						switch (FlowDirection)
						{
							case 9:
								//TODO: Error
								break;
							case 12:
								this.txtToActivity.Text = "End";
								//TODO: Error
								break;
						}
						break;

                    case ScenarioFramework.Modeler.Forms.BPMN.BPMNUtils.POOL_LANE_MASTER:
                        //find pool name
                        for (int i = 1; i <= o_Connect.ToSheet.Shapes.Count; i++)
                        {
                            string sShapeIdConnectchild = o_Connect.ToSheet.Shapes[i].Text;
                            if ( sShapeIdConnectchild.CompareTo (string.Empty ) !=  0) 
                            {
                                
                                switch (FlowDirection)
                                {
                                    case 9: // visBegin=9 Connection is from the begin point of a 1-D shape.
                                        // Set new transaction.from
                                        this.txtFromActivity.Text = sShapeIdConnectchild;
                                        break;
                                    case 12: //visEnd 12 Connection is from the end of a 1-D shape.
                                        // Set new transaction.to
                                        this.txtToActivity.Text = sShapeIdConnectchild;
                                        break;
                                }
                                break;
                            }
                        }
                        break;
                    case BPMNUtils.Gateway_MASTER:
                    case BPMNUtils.Activity_MASTER:
                    case BPMNUtils.COLLAPSED_SUBPROCESS_MASTER:
					default:
					{
						Visio.Shape oShape2=o_Connect.ToSheet;

						WorkflowProcesses.XMLActivity activity2Com = new WorkflowProcesses.XMLActivity();
						activity2Com.Parse ( oShape2.Data1);

						switch (FlowDirection)
						{
							case 9:
								this.txtFromActivity.Text=activity2Com.Name;
								break;

							case 12:
								this.txtToActivity.Text=activity2Com.Name;
								break;
						}
						break;
					}
				}
			}
             */
		}

        private void btnExtendedAttributes_Click(object sender, EventArgs e)
        {
            ExtendedAttributes frmAttributes = new ExtendedAttributes(xmlMessage.XMLExtendedAttributes);
            frmAttributes.ShowDialog(this);

            if (frmAttributes.Exit == FormExit.SAVE)
            {
                this.xmlMessage.XMLExtendedAttributes = frmAttributes.XMLExtendedAttributes;
            }
        }

        private void btnXML_Click(object sender, EventArgs e)
        {
            Scenario.Modeler.FormXML frmXML = new Scenario.Modeler.FormXML();
            frmXML.XML = this.xmlMessage.GetXML(true, 0);
            frmXML.ShowDialog(this);

            if (frmXML.OK)
            {
                xmlMessage.Parse(frmXML.XML);
            }
        }
	}
}
